package com.pet.server.ext.service;

import java.util.List;

import com.pet.server.ext.ApiError;
import com.pet.server.ext.constants.BaseAttribute;
import com.pet.server.ext.data.Page;
import com.pet.server.ext.data.ProductInfo;
import com.pet.server.ext.dbutil.JdbcTemplate;
import com.pet.server.ext.dbutil.Sql;


public class TbkService {
	private JdbcTemplate jdbcTemplate = new JdbcTemplate();
	
	
	
	public void searchTbkProductInfo(Page page,String pet_code,String cate_code, Double min_price,Double max_price,String[] keywords,String sort) throws ApiError{
		//Sql sql = new Sql("SELECT p.* FROM product_data p WHERE 1=1 ");
		Sql sql = new Sql("select p.*,(select group_concat(img.src separator ',') from `image` img where img.oid=p.id and img.`type`='product_data' ) images  from product_data p where 1=1 ");
		//sql.addParam(BaseAttribute.LOGIC_TRUE);
		    sql.appendLike("^", "AND", "p.product_cate", cate_code);
		    sql.append("and", "p.pet_cate", "=", pet_code);
		    //sql.appendLike(null, "and", "p.pet_cate",pet_code);
		    sql.append("and", "p.sale_price", ">=", min_price);
		    sql.append("and", "p.sale_price", "<=", max_price);
		    sql.appendLike(null, "and", "or", "p.product_name", keywords);
	        if("priceasc".equals(sort)){
	        	sql.append(" ORDER BY p.sale_price ASC");
	        }else if("pricedesc".equals(sort)){
	        	sql.append(" ORDER BY p.sale_price DESC");
	        }else{
	        	sql.append(" ORDER BY p.sort DESC,p.id DESC");
	        }
	        
	        System.out.println(pet_code);
	        
		jdbcTemplate.search(page, sql, ProductInfo.class);
	}
	
	public List<ProductInfo> queryTbkProductInfo(String pet_code,String cate_code, Double min_price,Double max_price,String[] keywords,String sort,Integer size) throws ApiError{
		//Sql sql = new Sql("SELECT p.* FROM product_data p WHERE 1=1 ");
		Sql sql = new Sql("select p.*,(select group_concat(img.src separator ',') from `image` img where img.oid=p.id and img.`type`='product_data' ) images  from product_data p where 1=1 ");
		//sql.addParam(BaseAttribute.LOGIC_TRUE);
		    sql.appendLike("^", "AND", "p.product_cate", cate_code);
		    sql.append("and", "p.pet_cate", "=", pet_code);
		    sql.append("and", "p.sale_price", ">=", min_price);
		    sql.append("and", "p.sale_price", "<=", max_price);
		    sql.appendLike(null, "and", "or", "p.product_name", keywords);
	        if("priceasc".equals(sort)){
	        	sql.append(" ORDER BY p.sale_price ASC");
	        }else if("pricedesc".equals(sort)){
	        	sql.append(" ORDER BY p.sale_price DESC");
	        }else{
	        	sql.append(" ORDER BY p.sort DESC,p.id DESC");
	        }
	        
		return jdbcTemplate.queryForList( sql, ProductInfo.class,size);
	}
	
	
	public List<ProductInfo> queryProductData(Integer size) throws ApiError{
		Sql sql = new Sql("SELECT p.* FROM product_data p WHERE 1=1");
	        sql.append(" ORDER BY p.id DESC");
		return jdbcTemplate.queryForList( sql, ProductInfo.class,size);
	} 
	
	
	public void stest(String[] args){
		Sql sql = new Sql("select * from a where 1=1");
		    sql.appendLike(null, "and", "or", "a.colment", args);
		    System.out.println(sql.toString());
	}
	
	public static void main(String[] args){
		
		String str="a b";
		String[] strs = str.split("\\s");

		for(String s:strs){
			System.out.println(s);
		}
		
		TbkService tbkService = new TbkService();
		tbkService.stest(new String[]{"a","b"});
		
		
	}
	
	
}
